//
//  BookViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/13.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "BookViewController.h"
#import "DepartOneViewController.h"
#import "GroupListViewController.h"
#import "MyTableViewCell.h"

@interface BookViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)UIImage *image0;
@property (nonatomic,strong)UIImage *image1;

@end

@implementation BookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)customViewWillAppear{
    
   
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self.tableView reloadData];
}
- (UIView *)headView{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 100)];
    headerView.backgroundColor = KWhiteColor;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15,50,50)];
    imageView.image = [UIImage imageNamed:@"xiaoxi_logo"];
    [self.view addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(imageView.right+5, imageView.top, kSCREEN_WIDTH-10-50-10, 50)];
    lable.font = FONT(20);
    lable.textColor = [UIColor blackColor];
    [headerView addSubview:lable];

    lable.text = [DataCenter shareInstence].companyName;
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, headerView.bottom-1, kSCREEN_WIDTH, 1)];
    lineView.backgroundColor = KLineColor;
    [self.view addSubview:lineView];
    
    return headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section == 0){
        return [DataCenter shareInstence].companyList.count;
    }else{
        return 1;
    }

}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 35)];
    lable.backgroundColor = KLineColor;
    lable.font = FONT(13);
    lable.textColor = [UIColor colorWithHexString:@"666666"];
    if (section == 0) {
        lable.text = @"      组织机构";
    }
    if (section == 1) {
        lable.text = @"      我的群组";
    }
    return lable;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyTableViewCell *tableCell = [MyTableViewCell cellWithTableView:tableView];
    if(indexPath.section == 0){
        
        NSArray *companyModelArr = [DataCenter shareInstence].companyList;
        CompanyModel *model = companyModelArr[indexPath.row];
        tableCell.imageName.image = self.image0;
        tableCell.title.text = model.name;
    }
    if(indexPath.section == 1){
        tableCell.title.text = @"我的群组";
        tableCell.imageName.image = self.image1;
    }
    
    
    return tableCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSArray *companyModelArr = [DataCenter shareInstence].companyList;
        CompanyModel *model = companyModelArr[indexPath.row];
        DepartOneViewController *departVC = [[DepartOneViewController alloc] init];
        departVC.companyId = model.companyId;
        [self.navigationController pushViewController:departVC animated:YES];
        
    }
    if (indexPath.section == 1){
        //获取群组
        GroupListViewController *groupListVC = [[GroupListViewController alloc] init];
        [self.navigationController pushViewController:groupListVC animated:YES];
        
    }
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (UIImage *)image0{
    if(!_image0){
        _image0 = [UIImage imageNamed:@"xiaoxi_logo"];
    }
    return _image0;
}
- (UIImage *)image1{
    if(!_image1){
        UILabel *label = [UILabel labelWithFrame:CGRectMake(0, 0, 35, 35) Font:FONT(20) TextColor:KWhiteColor];
        label.textAlignment = 1;
        label.text = @"群";
        label.backgroundColor = [UIColor colorWithHexString:@"2c9ae8"];
        _image1 = [Tool imageWithUIView:label];
    }
    return _image1;
}
@end
